package com.yh.travel.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yh.travel.core.pojo.BaseEntity;
import com.yh.travel.core.pojo.ResponseBean;
import com.yh.travel.core.service.ICrudService;
import com.yh.travel.core.utils.GenericUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName BaseController
 * @Description Controller基类
 * @Author 0715-YuHao
 * @Date 2020/10/31 11:20
 */
public abstract class BaseController<S extends ICrudService<T>, T extends BaseEntity> {

    @Autowired
    protected S service;

    protected Class<T> entityClass;

    public BaseController() {
        this.entityClass = GenericUtil.getSuperGenericClass(this.getClass());
    }

    /**
     * @Author 0715-YuHao
     * @Description 加载
     * @Date 2020/10/31 11:26
     * @Param [id]
     * @return T
     */
    @ApiOperation(value="加载", notes="根据ID加载")
    @GetMapping("/edit/{id}")
    public T edit(@PathVariable Long id) throws Exception {
        T entity = service.getById(id);
        afterEdit(entity);
        return entity;
    }

    /**
     * @Author 0715-YuHao
     * @Description 分页查询
     * @Date 2020/10/31 11:26
     * @Param [entity, page, rows]
     * @return IPage<T>
     */
    @ApiOperation(value="分页查询", notes="分页查询")
    @PostMapping("/list-page")
    public IPage<T> listPage(T entity,
                             @RequestParam(name = "page", defaultValue = "1", required = false)
                                     long page,
                             @RequestParam(name = "rows", defaultValue = "10", required = false)
                                     long rows) {
        Page<T> p = new Page<>(page, rows);
        return service.listPage(p, entity);
    }

    /**
     * @Author 0715-YuHao
     * @Description 查询所有
     * @Date 2020/10/31 11:28
     * @Param []
     * @return List<T>
     */
    @ApiOperation(value="查询", notes="查询所有")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public List<T> listAll() {
        return service.list();
    }

    /**
     * @Author 0715-YuHao
     * @Description 增加，修改
     * @Date 2020/10/31 11:29
     * @Param [entity]
     * @return ResponseBean
     */
    @ApiOperation(value="保存", notes="ID存在修改，不存在添加")
    @PostMapping("/save")
    public ResponseBean save(T entity) throws Exception {
        ResponseBean rm = new ResponseBean();
        try {
            //保存前处理实体类
            beforeSave(entity);
            service.saveOrUpdate(entity);
            rm.setModel(entity);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setSuccess(false);
            rm.setMsg("保存失败");
        }
        return rm;
    }

    /**
     * @Author 0715-YuHao
     * @Description 删除
     * @Date 2020/10/31 11:30
     * @Param [id]
     * @return com.yh.travel.core.pojo.ResponseBean
     */
    @ApiOperation(value="删除", notes="根据ID删除")
    @GetMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws Exception {
        ResponseBean rm = new ResponseBean();
        try {
            service.removeById(id);
            rm.setModel(null);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setSuccess(false);
            rm.setMsg("保存失败");
        }
        return rm;
    }

    /**
     * @Author 0715-YuHao
     * @Description 批量删除
     * @Date 2020/10/31 11:31
     * @Param [ids]
     * @return com.yh.travel.core.pojo.ResponseBean
     */
    @ApiOperation(value="删除", notes="批量删除")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseBean delete(@RequestParam List<Long> ids) {
        ResponseBean rm = new ResponseBean();
        try {
            service.removeByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMsg("删除失败");
            rm.setSuccess(false);
        }
        return rm;
    }

    /**
     * 保存前执行
     */
    public void beforeSave(T entity) throws Exception {
    }
    /**
     * 模板方法：在加载后执行
     */
    public void afterEdit(T entity) {
    }
}
